package cn.lysj.framework.store.service;

import cn.hutool.core.util.ImageUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.lysj.framework.base.BaseService;
import cn.lysj.framework.common.CommonConstant;
import cn.lysj.framework.common.domain.User;
import cn.lysj.framework.common.repository.UserRepository;
import cn.lysj.framework.common.service.UserService;
import cn.lysj.framework.common.utils.qrcode.QrCode;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.store.domain.EatTable;
import cn.lysj.framework.store.repository.EatTableRepository;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

/**
 * @author drj
 * @description 餐桌服务层
 * @create 2018-11-02 10:15:54
 **/
@Slf4j
@Service
@Transactional
public class EatTableService implements BaseService<EatTable> {
    @Value("${qr.code.url.prefix}")
    public String qrCodePrefix;

    @Resource
    private EatTableRepository eatTableRepository;
    @Resource
    private UserService userService;

    @Override
    public EatTableRepository getRepository() {
        return eatTableRepository;
    }


    public Resp saveRewrite(EatTable eatTable) {
        int num = eatTableRepository.countByStoreIdAndSerialNumberAndDelFlag(eatTable.getStoreId(), eatTable.getSerialNumber(),  CommonConstant.NORMAL_FLAG);
        if (num > 0) {
            return new Resp(Resp.PARAM_ERROR, "餐桌号已存在,请重新设置");
        }
        eatTable.setCreateTime(new Date());
        eatTable.setUpdateTime(new Date());
        eatTable = eatTableRepository.save(eatTable);

        String url = qrCodePrefix + "?eatTableId=" + eatTable.getId() + "&storeId=" + eatTable.getStoreId(); //二维码内容
        eatTable.setQrCode(QrCode.createQrCode(url));
        return new Resp("添加成功");
    }
}
